package com.kecheng.xinmei.model;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.web3j.crypto.*;
import org.web3j.utils.Numeric;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
public class DexOrderDomain {
    private Types types;
    private String primaryType;
    private Domain domain;
    private Message message;
    @Data
    public static class Message {
        private String nftAddress;
        private String ownerAddress;
        private BigInteger tokenId;
        private BigInteger amount;
    }
    @Data
    public static class Domain {
        private String name;
        private String version;
        private String verifyingContract;
    }
    @Data
    public static class Types {
        private List<EIP712Domain> EIP712Domain;
        private List<Order> order;
    }
    @Data
    public static class Order {
        private String name;
        private String type;
    }
    @Data
    public static class EIP712Domain {
        private String name;
        private String type;
    }
    public static DexOrderDomain init(String orderName,String OrderVersion,String orderContract){
        DexOrderDomain dexOrderDomain = new DexOrderDomain();
        Types types = new Types();
        List<Order> signList = new ArrayList<>();
        Order sign1 = new Order();
        sign1.setName("nftAddress");
        sign1.setType("address");
        Order sign2 = new Order();
        sign2.setName("ownerAddress");
        sign2.setType("address");
        Order sign3 = new Order();
        sign3.setName("tokenId");
        sign3.setType("uint256");
        Order sign4 = new Order();
        sign4.setName("amount");
        sign4.setType("uint256");
        signList.add(sign1);
        signList.add(sign2);
        signList.add(sign3);
        signList.add(sign4);
        types.setOrder(signList);
        List<EIP712Domain> eip712DomainList = new ArrayList<>();
        EIP712Domain name = new EIP712Domain();
        name.setName("name");
        name.setType("string");
        EIP712Domain version = new EIP712Domain();
        version.setName("version");
        version.setType("string");
        EIP712Domain verifyingContract = new EIP712Domain();
        verifyingContract.setName("verifyingContract");
        verifyingContract.setType("address");
        eip712DomainList.add(name);
        eip712DomainList.add(version);
        eip712DomainList.add(verifyingContract);
        types.setEIP712Domain(eip712DomainList);
        dexOrderDomain.setTypes(types);
        dexOrderDomain.setPrimaryType("Order");
        Domain domain = new Domain();
        domain.setName(orderName);
        domain.setVersion(OrderVersion);
        domain.setVerifyingContract(orderContract);
        dexOrderDomain.setDomain(domain);
        return dexOrderDomain;
    }
    public static void main(String[] args) throws IOException {
        DexOrderDomain dexOrderDomain =
                init("Exchange", "1", "0x8198FE4292A8420656981417Bd025d73b173a8F8");
        DexOrderDomain.Message message = new DexOrderDomain.Message();
        message.setNftAddress("0x394A64e586FC05bD28783351F14dfcc426EFD230");
        message.setOwnerAddress("0xed9a49977b8a12dc050169933a57999a2415a930");
        message.setTokenId(new BigInteger("500000000000000000"));
        message.setAmount(new BigInteger("500000000000000000"));
        dexOrderDomain.setMessage(message);
        String s = JSONObject.toJSONString(dexOrderDomain);
        String s1 = s.replaceAll("eIP712Domain", "EIP712Domain");
        String s2 = s1.replaceAll("\"order\"", "\"Order\"");
        System.out.println(s2);
    }

}
